package com.okyai.kt.controller;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.okyai.common.annotation.Log;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.core.domain.entity.SysUser;
import com.okyai.common.core.page.TableDataInfo;
import com.okyai.common.enums.BusinessType;
import com.okyai.common.utils.ShiroUtils;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.kt.domain.Record;
import com.okyai.kt.domain.RecordDetail;
import com.okyai.kt.domain.RecordPlan;
import com.okyai.kt.domain.Topic;
import com.okyai.kt.domain.vo.PlanExportVo;
import com.okyai.kt.service.IRecordDetailService;
import com.okyai.kt.service.IRecordPlanService;
import com.okyai.kt.service.IRecordService;
import com.okyai.kt.service.ITopicService;
import com.okyai.qa.domain.Notes;
import com.okyai.qa.domain.Questionnaire;
import com.okyai.qa.service.INotesService;
import com.okyai.qa.service.IQuestionnaireService;

/**
 * 档案Controller
 * 
 * @author okyai
 * @date 2021-03-20
 */
@Controller
@RequestMapping("/kt/record")
public class RecordController extends BaseController
{
    private String prefix = "kt/record";

    @Autowired
    private IRecordService recordService;
    
    @Autowired
    private ITopicService topicService;
    
    @Autowired
    private IRecordDetailService recordDetailService;
    
    @Autowired
    private IQuestionnaireService questionnaireService;
    
    @Autowired
    private INotesService notesService;
    
    @Autowired
    private IRecordPlanService recordPlanService;
    
    @RequiresPermissions("kt:record:view")
    @GetMapping()
    public String record(ModelMap mmap)
    {
    	Topic topic = new Topic();
    	SysUser user = ShiroUtils.getSysUser();
    	topic.setDeptId(user.getDeptId());
    	mmap.put("topicList", topicService.selectTopicList(topic));
        return prefix + "/record";
    }

    /**
     * 查询档案列表
     */
    @RequiresPermissions("kt:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Record record)
    {
        startPage();
        List<Record> list = recordService.selectRecordList(record);
        return getDataTable(list);
    }

    /**
     * 导出档案列表
     */
    @Log(title = "档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Long recordPlanId)
    {
    	//RecordPlan plan =  recordPlanService.selectRecordPlanById(recordPlanId);
    	List<PlanExportVo> list = recordPlanService.export(recordPlanId);
        ExcelUtil<PlanExportVo> util = new ExcelUtil<PlanExportVo>(PlanExportVo.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增档案
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	Topic topic = new Topic();
    	SysUser user = ShiroUtils.getSysUser();
    	topic.setDeptId(user.getDeptId());
    	mmap.put("topicList", topicService.selectTopicList(topic));
    	mmap.put("now", new Date());
        return prefix + "/add";
    }

    /**
     * 新增保存档案
     */
    @RequiresPermissions("kt:record:add")
    @Log(title = "档案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Record record)
    {
        return toAjax(recordService.insertRecord(record));
    }

    /**
     * 修改档案
     */
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap)
    {
        Record record = recordService.selectRecordById(recordId);
        mmap.put("record", record);
        return prefix + "/edit";
    }

    /**
     * 修改保存档案
     */
    @RequiresPermissions("kt:record:edit")
    @Log(title = "档案", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Record record)
    {
        return toAjax(recordService.updateRecord(record));
    }

    /**
     * 删除档案
     */
    @RequiresPermissions("kt:record:remove")
    @Log(title = "档案", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordService.deleteRecordByIds(ids));
    }
    
    
    @GetMapping("/study/{recordId}")
    public String study(@PathVariable("recordId") Long recordId, ModelMap mmap) {
    	Record record = recordService.selectRecordById(recordId);
        mmap.put("record", record);
        Topic topic = recordService.selectRecordItem(record);
        mmap.put("topic", topic);
        RecordPlan recordPlan = new RecordPlan();
        recordPlan.setRecordId(recordId);
        List<RecordPlan> planList = recordPlanService.selectRecordPlanList(recordPlan);
        if(planList.size()>0) {
        	planList = planList.stream().sorted(Comparator.comparing(RecordPlan::getPlanId)).collect(Collectors.toList());
        }
        mmap.put("planList", planList);
    	return prefix + "/study";
    }
    
    @GetMapping("/studyMain/{detailId}")
    public String studyMain(@PathVariable("detailId") Long detailId, ModelMap mmap) {
    	//查询所有的RecordDetail 以及当前detail的ID，
    	RecordDetail detail = recordDetailService.selectRecordDetailById(detailId);
    	RecordDetail key = new RecordDetail();
    	key.setRecordPlanId(detail.getRecordPlanId());
    	List<RecordDetail> list = recordDetailService.selectRecordDetailList(key);
    	//查询问卷以及答案
    	if(detail.getDetailType().equals("W")) {
    		Questionnaire questionnaire = questionnaireService.questionnaireInfo(detail.getDetailTypeId(),detail.getDetailTypeInfo());
        	mmap.put("questionnaire", questionnaire);
    	}else {
    		
    	}
    	mmap.put("detail", detail);
    	mmap.put("detailList", list);
    	return prefix + "/studyMain";
    }
    
    @PostMapping("/saveDetail")
    @ResponseBody
    public AjaxResult saveDetail(HttpServletRequest request) {
    	//查询所有的RecordDetail 以及当前detail的ID，
    	Long detailId = Long.parseLong(request.getParameter("detailId"));
    	Map<String,String[]> map=request.getParameterMap(); 
    	//保存表单内容 修改状态
    	Notes notes = new Notes();
    	notes.setNotesSource("002");
    	notesService.answer(notes, map);
    	RecordDetail detail = recordDetailService.selectRecordDetailById(detailId);
    	detail.setDetailTypeInfo(notes.getNotesId());
    	detail.setDetailStatus("1");
    	return toAjax(recordDetailService.updateRecordDetail(detail));
    }
    
    @GetMapping("/queryPerson")
    public String queryPerson() {
    	return prefix + "/queryPerson";
    }
    
    
}
